package org.duanyu.shopping_order_customer_api.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.duanyu.shopping_common.pojo.CartGoods;
import org.duanyu.shopping_common.pojo.Orders;
import org.duanyu.shopping_common.result.BaseResult;
import org.duanyu.shopping_common.service.CartService;
import org.duanyu.shopping_common.service.OrdersService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/orders")
public class OrderController {

    @DubboReference
    private OrdersService ordersService;

    @DubboReference
    private CartService cartService;

    /**
     * 添加订单
     *
     * @param orders 订单实体
     * @param userId 用户id
     * @return 执行结果
     */
    @PostMapping("/add")
    public BaseResult<Orders> add(@RequestBody Orders orders, @RequestHeader Long userId) {
        //保存订单
        orders.setUserId(userId);
        Orders orders1 = ordersService.add(orders);
        //将redis中购物车商品删除
        List<CartGoods> cartGoods = orders.getCartGoods();
        for (CartGoods cartGood : cartGoods) {
            cartService.deleteCartOption(userId, cartGood.getGoodId());
        }
        return BaseResult.success(orders1);
    }

    /**
     * 根据商品id查询商品
     *
     * @param id 商品id
     * @return 执行结果
     */
    @GetMapping("/findById")
    public BaseResult<Orders> findById(String id) {
        return BaseResult.success(ordersService.findById(id));
    }

    /**
     * 查找用户所有订单
     *
     * @param userId 用户id
     * @param status 订单状态 1、未付款，2、已付款，3、未发货，4、已发货，5、交易成功，6、交易关闭,7、待评价
     * @return 执行结果
     */
    @GetMapping("/findUserOrders")
    public BaseResult<List<Orders>> findUserOrders(@RequestHeader Long userId, Integer status) {
        List<Orders> userOrders = ordersService.findUserOrders(userId, status);
        return BaseResult.success(userOrders);
    }

}
